package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyTagService strategyTagService;

    @Reference
    private ITravelService travelService;


    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions", regionService.queryHotRegions());
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, Long regionId){

        //1:查询数据
        List<Destination> list = destinationService.getDestByRegionId(regionId);

        //2:拆分数据
        int size = list.size();

        //左边:leftDests
        model.addAttribute("leftDests",list.subList(0, size / 2 + 1) );

        //右边:rightDests
        model.addAttribute("rightDests",list.subList(size / 2 + 1,size ));
        //3:拼接html模板
        model.addAttribute("regionId", regionId);


        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public Object guide(Model model,Long id){
        //获取吐司数据
        List<Destination> toasts = destinationService.getToasts(id);
        model.addAttribute("toasts",toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);

        //通过地点id获取攻略分类
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);


        //查询指定目的地前三条
        //strategyDetails
        List<StrategyDetail> viewNumTop3 = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails",viewNumTop3);
        return "/destination/guide";
    }

    @RequestMapping("/surveyPage")
    public Object surveyPage(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo){
        //
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        model.addAttribute("toasts",toasts);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("dest",dest);
        return "/destination/survey";
    }

    @RequestMapping("/survey")
    public Object survey(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo){

        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        StrategyCatalog  catalog = null;
        for (StrategyCatalog c : catalogs) {
            if (c.getId() == qo.getCatalogId()){
                catalog = c;
                model.addAttribute("catalog",catalog);
            }
        }

        //detail
        if (catalog!=null){
            List<StrategyDetail> details = catalog.getDetails();
            if (details !=null && details.size()>0){
                StrategyDetail detail = details.get(0);
                detail.setStrategyContent(strategyDetailService.getContent(detail.getId()));
                model.addAttribute("detail",details.get(0));
            }
        }

        return "/destination/surveyTpl";
    }


    //查看游记
    @RequestMapping("travels")
    public Object list(Model model, @ModelAttribute("qo") TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "/destination/travelTpl";
    }

}
